package com;

import obj.Student;
import obj.Teacher;
import util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet(value = "/InfoServlet")
public class InfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        int u_id= (int) request.getSession().getAttribute("u_id");
        String role= (String) request.getSession().getAttribute("role");
        ArrayList list=new ArrayList();
        Connection con=null;
        PreparedStatement psm=null;
        ResultSet rs=null;

        try {

            switch (role){
                        case "student":
                            //System.out.println("学生");
                            con = JdbcUtil.getConnection();
                            psm = con.prepareStatement("SELECT * FROM student s JOIN USER u ON s.`s_id`=u.`u_id` WHERE u.`u_id`="+u_id);
                            rs=psm.executeQuery();
                            while (rs.next()){
                                Student student=new Student(rs.getInt("s_id"),rs.getString("s_name"),rs.getInt("s_age"),rs.getString("s_sex"),rs.getString("s_class"));
                                list.add(student);
                            }
                            request.setAttribute("lists",list);
                            request.getRequestDispatcher("/jsp/StudentInfo.jsp").forward(request,response);
                            rs.close();
                            break;

                        case "teacher":
                            //System.out.println("老师");
                            con = JdbcUtil.getConnection();
                            psm = con.prepareStatement("SELECT * FROM teacher t JOIN USER u ON t.`t_id`=u.`u_id` WHERE u.`u_id`="+u_id);
                            rs=psm.executeQuery();
                            while (rs.next()){
                                Teacher teacher=new Teacher(rs.getInt("t_id"),rs.getString("t_name"),rs.getString("t_sex"),rs.getString("t_sub"));
                                list.add(teacher);
                            }
                            request.setAttribute("lists",list);
                            request.getRequestDispatcher("/jsp/TeacherInfo.jsp").forward(request,response);
                            rs.close();
                            break;
                        case "admin":
                            //System.out.println("管理员");

                            break;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    }


